package com.kt.app.ui.components


import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Checkbox
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun RadioButtonSample() {
    // 单选按钮
//    var selected by remember { mutableStateOf(false) }
//    RadioButton(selected,  onClick = { selected = !selected })
        val ctx = LocalContext.current
      var checkedList by  remember { mutableStateOf(listOf(false, false, false)) }
        Column {
            checkedList.forEachIndexed { index,item ->
                RadioButton(item,  onClick = {
                    checkedList=checkedList.mapIndexed{idx,_->
                        idx==index
                    }
                    Toast.makeText(ctx,"点击了第${checkedList}个单选按钮",Toast.LENGTH_SHORT).show()
                }
                )
            }
        }
}

@Preview
@Composable
fun RadioButtonSamplePreview() {
    RadioButtonSample()
}

@Composable
fun CheckBoxSample() {
    val items = listOf("苹果","香蕉","橘子")
    var selectIndex by remember { mutableStateOf(0) }
    Column {
        items.forEachIndexed{index,item->
            Row (
                modifier = Modifier.background(Color.Gray).fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically,

                ){
                Checkbox(index==selectIndex, onCheckedChange = { selectIndex = index })
                Text("苹果")
            }
        }
    }


}

@Preview
@Composable
fun CheckBoxSamplePreview() {
    CheckBoxSample()
}

@Composable
fun <T> SelectableList(
    items: List<T>,
    selectedPosition: Int,
    onItemSelected: (Int) -> Unit,
    modifier: Modifier = Modifier,
    itemContent: @Composable (T, Boolean) -> Unit
) {
    Column(modifier = modifier) {
        items.forEachIndexed { index, item ->
            Row(
                modifier = Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Checkbox(
                    checked = index == selectedPosition,
                    onCheckedChange = { onItemSelected(index) }
                )
                itemContent(item, index == selectedPosition)
            }
        }
    }
}

@Composable
@Preview
fun CheckBoxSample2() {
    val items = listOf("苹果", "香蕉", "橘子")
    var selectIndex by remember { mutableStateOf(0) }

    SelectableList(
        items = items,
        selectedPosition = selectIndex,
        onItemSelected = { selectIndex = it },
        itemContent = { item: String, isSelected: Boolean ->
            Text(text = item)
        }
    )
}

